package com.lili.math;

/**
 * @Auther: 李 力
 * @Date: 2024/8/23
 * @Description: 超级次方
 * @version: 1.0
 */
public class LeetCode372 {

    public int superPow(int a, int[] b) {
        int ans = 1;
        for (int i = b.length - 1; i >= 0; --i) {
            ans = (int) ((long) ans * pow(a, b[i]) % 1337);
            a = pow(a, 10);
        }
        return ans;
    }


    private static int pow(int x, int n) {
        int res = 1;
        while (n != 0) {
            if (n % 2 != 0) {
                res = (int) ((long) res * x % 1337);
            }
            x = (int) ((long) x * x % 1337);
            n = n / 2;
        }
        return res;
    }
}
